package com.dev.module_zqc_sleep.fragment

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Build
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.dev.module_ui_composable.theme.JetComposableTheme
import com.dev.module_ui_composable.utils.ToastUtils
import com.dev.module_ui_composable.view.AbstractComposeFragment
import com.dev.module_zqc_sleep.activity.SettingAlarmActivity
import com.dev.module_zqc_sleep.mvi.ui.AlarmClockPage
import com.dev.module_zqc_sleep.mvi.vm.AlarmViewModel
import com.fwlst.lib_base.utils.AdUtils
import com.fwlst.lib_base.utils.JtXXPermissions
import com.hjq.permissions.Permission


@Route(path = "/module_sleep/route/AlarmClockFragment")
class AlarmClockFragment : AbstractComposeFragment() {

    private val alarmViewModel by viewModels<AlarmViewModel>()

    private val startForResult =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
                val data = result.data?.getStringExtra("resultKey")
                if (data != null) {
                    alarmViewModel.updateSelectedTime(requireContext(), data)
                }
            }
        }

    @Composable
    override fun ComposeContent() {

        AlarmClockPage(
            alarmViewModel = alarmViewModel,
            onNavigateSetting = {
                navigateActivity<SettingAlarmActivity>()
            },
            onStartAlarm = { isAlarmEnabled, selectedTime ->
                startAlarm(isAlarmEnabled, selectedTime)
            }
        )

    }

    private fun startAlarm(isAlarmEnabled: Boolean, selectedTime: String) {
//        val pre: MutableList<String> = ArrayList()
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
//            pre.add(Manifest.permission.POST_NOTIFICATIONS)
//        }
        val list = listOf(Permission.POST_NOTIFICATIONS)
        JtXXPermissions.requestPermission(requireContext(), "", list) {
            if (!isAlarmEnabled && selectedTime.isNotEmpty()) {
                AdUtils.checkFunction(requireContext()) {
                    alarmViewModel.toggleAlarmEnabled(requireContext())
                    alarmViewModel.parseTimeAndSetAlarm(requireContext(), selectedTime)
                    ToastUtils.showShortToast(requireContext(), "闹钟已设置")
                }
            } else {
                alarmViewModel.toggleAlarmEnabled(requireContext())
                alarmViewModel.cancelAlarm(requireContext())
                ToastUtils.showShortToast(requireContext(), "闹钟已取消")
            }
        }

    }

    private inline fun <reified T : Activity> navigateActivity() {
        val intent = Intent(requireContext(), T::class.java)
        startForResult.launch(intent)
    }

}